package com.example;

import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;

/**
 * <p>绘画记录栈</p>
 *
 * @author dc
 * @date 2025-09-29
 **/
public class DrawStackManager {
    private PaneManager paneManager;
    private final AnchorPane drawShapeStack = new AnchorPane();
    
    public DrawStackManager(PaneManager paneManager) {
        this.paneManager = paneManager;
        drawShapeStack.setBackground(null);
        drawShapeStack.setMouseTransparent(true);
        drawShapeStack.setBorder(Border.stroke(Color.RED));
    }
    
    public AnchorPane getDrawShapeStack() {
        return drawShapeStack;
    }

    /**
     * 更改绘制栈布局的位置
     * @param x 选中区域的左上角X坐标
     * @param y 选中区域的左上角Y坐标
     * @param width 选中区域的宽度
     * @param height 选中区域的高度
     */
    public void changeStackPosition(double x, double y, double width, double height) {
        AnchorPane.setLeftAnchor(drawShapeStack, x);
        AnchorPane.setTopAnchor(drawShapeStack, y);
        AnchorPane.setRightAnchor(drawShapeStack, CaptureProperty.screenWidth - (x + width));
        AnchorPane.setBottomAnchor(drawShapeStack, CaptureProperty.screenHeight - (y + height));
        drawShapeStack.setVisible(true);

        // 设置相对于AnchorPane的裁剪区域，隐藏超出区域的绘图
        Rectangle rectangle = new Rectangle(0, 0, width, height);
        rectangle.setMouseTransparent(true);
        drawShapeStack.setClip(rectangle);
    }
    
    public void addShape(Shape shape) {
        drawShapeStack.getChildren().add(shape);
    }
    public void removeShape() {
        ObservableList<Node> children = drawShapeStack.getChildren();
        if (children.size() > 0) {
            children.remove(children.size() - 1);
        }
    }
    public void clearStack() {
        drawShapeStack.getChildren().clear();
    }
    
    
    
    
    
    
    
    
    
}
